﻿using System.Linq;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace Sustainalytics.ReportGenerator.BL
{
    public class HistoricalPerformanceChart
    {
        public SeriesPoints BestInClass { get; set; }
        public SeriesPoints CompanyScores { get; set; }
        public SeriesPoints IndustryAverage { get; set; }

        public bool ShowArea1AxisY { get; set; }
        public bool ShowArea2AxisY { get; set; }

        public bool LargeChart { get; set; }
        public bool Calibration { get; set; }


        public HistoricalPerformanceChart()
        {
            ShowArea1AxisY = true;
            ShowArea2AxisY = true;
            LargeChart = true;
            Calibration = false;
        }

        public byte[] GetChartImage(int width, int height)
        {
            using (var chartTemplate = new HistoricalPerformanceChartTemplate(width, height, false))
            {
                if (BestInClass != null)
                {
                    chartTemplate.Series[0].Points.Clear();

                    for (var i = 0; i < BestInClass.YValues.Count(); i++)
                    {
                        var dataPoint = new Dundas.Charting.WinControl.DataPoint
                        {
                            XValue = BestInClass.XValues[i],
                            YValues = new[] {BestInClass.YValues[i], 0}
                        };
                        chartTemplate.Series[0].Points.Add(dataPoint);
                    }
                }

                if (IndustryAverage != null)
                {
                    chartTemplate.Series[1].Points.Clear();

                    for (var i = 0; i < IndustryAverage.YValues.Count(); i++)
                    {
                        var dataPoint = new Dundas.Charting.WinControl.DataPoint
                        {
                            XValue = IndustryAverage.XValues[i],
                            YValues = new[] {IndustryAverage.YValues[i], 0}
                        };
                        chartTemplate.Series[1].Points.Add(dataPoint);
                    }
                }

                if (CompanyScores != null)
                {
                    chartTemplate.Series[2].Points.Clear();

                    for (var i = 0; i < CompanyScores.YValues.Count(); i++)
                    {
                        var dataPoint = new Dundas.Charting.WinControl.DataPoint
                        {
                            XValue = CompanyScores.XValues[i],
                            YValues = new[] {CompanyScores.YValues[i], 0}
                        };

                        chartTemplate.Series[2].Points.Add(dataPoint);
                    }
                }

                // Create new bitmap where chart will be printed
                using (var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb))
                {
                    // Create graphics object from bitmap.
                    using (var imageGraphics = Graphics.FromImage(bitmap))
                    {
                        // Print chart to the bitmap object.
                        var rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                        chartTemplate.Printing.PrintPaint(imageGraphics, rectangle);
                    }

                    using (var stream = new MemoryStream())
                    {
                        bitmap.Save(stream, ImageFormat.Png);
                        var buffer = stream.ToArray();
                        return buffer;
                    }
                }
            }
        }
    }
}
